package com.raising.modules.sys.service;

import com.raising.framework.service.CrudService;
import com.raising.modules.sys.dao.SwordDao;
import com.raising.modules.sys.entity.Sword;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 敏感词表
 Service层
* @author gaoy
* @version 1.0
*/
@Service
@Transactional(readOnly = true)
public class SwordService extends CrudService<SwordDao, Sword> {
    /**
     * 查找所有敏感词
     * @author fangl
     * 2017-5-26 下午4:32:34
     * @return
     */
    public List<Sword> findList(){
        return dao.findList();
    }
    /**
     * 批量插入敏感词
     * @author gaoy
     * 2017 2017-6-2 下午1:01:37
     * @param list
     */
    @Transactional(readOnly = false)
    public void saveMuti(List<Sword> list){
        int batchNum = 1000;//批处理一次提交数量
//		int successNum = 0;
        int allNum = list.size();
        List<Sword> importList = new ArrayList<Sword>();
        try {
            for(int i=0;i<allNum;i++){
                importList.add(list.get(i));
//				successNum ++;
                if(i>0 && i%batchNum == 0 || i == allNum-1){//每1000提交一次
                    Map<String,Object> map = new HashMap<String,Object>();
                    map.put("list", importList);
                    dao.saveMuti(map);
                    importList.clear();//清除list数据
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
